#include "sourceforgeprojects.h"

#include <httpclient.h>

SourceForgeProjects::SourceForgeProjects() {}

QString SourceForgeProjects::name()
{
    return "SourceForge - Projects";
}

QString SourceForgeProjects::prefix()
{
    return "https://sourceforge.net";
}

bool SourceForgeProjects::autoLoad()
{
    return false;
}

Wiki::SearchMode SourceForgeProjects::searchMode()
{
    return ListItem;
}

QList<QListWidgetItem *> SourceForgeProjects::searching_item(const QString &text)
{
    QList<QListWidgetItem*> list;

    Request req("https://sourceforge.net/proxy-api/search/suggest-project");
    req.addQuery("q", text);
    auto items = HttpClient::instance().get(req).toMap().value("response").toMap().value("docs").toList();
    foreach (auto item, items) {
        auto var = item.toMap();

        auto name = var.value("name").toString();
        auto project_url = var.value("project_url").toString();
        auto icon_url = var.value("icon_url").toString();

        auto v = newItem();
        v->setText(name);
        v->setData(990, project_url);
        list << v;
    }

    return list;
}

Wiki::ContentMode SourceForgeProjects::contentMode()
{
    return Url;
}

QString SourceForgeProjects::content_item(const QListWidgetItem *item)
{
    return prefix() + item->data(990).toString();
}
